package com.qys.livesMall.shop.scheduledTask.task;

import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.model.entity.H2AppUserRechargeWithdrawals;
import com.qys.livesMall.app.user.service.IH2AppUserRechargeWithdrawalsService;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.common.utils.DateUtils;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.event.OperateReportFormsEvent;
import com.qys.livesMall.shop.mapper.H2ShopRechargeWithdrawalMapper;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.H2ShopRechargeWithdrawal;
import com.qys.livesMall.shop.modal.entity.ShopOrderInfo;
import com.qys.livesMall.shop.modal.entity.SysOperateReportForms;
import com.qys.livesMall.shop.service.ShopOrderInfoService;
import com.qys.livesMall.shop.service.SysOperateReportFormsService;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 报表管理-运营报表
 */
@Component("SysOperateReportFormsTask")
@Slf4j
@AllArgsConstructor
public class SysOperateReportFormsTask {
    @Resource
    private IH2AppUserRechargeWithdrawalsService userRechargeService;
    @Resource
    private H2ShopRechargeWithdrawalMapper rechargeWithdrawalMapper;
    @Resource
    private IH2AppUserService ih2AppUserService;
    @Resource
    private H2ShopBaseDao h2ShopBaseDao;
    @Resource
    private ShopOrderInfoService shopOrderInfoService;
    @Resource
    private ApplicationContext applicationContext;
    public void operateReportFormsTask(){
        SysOperateReportForms reportForms=new SysOperateReportForms();
        Date date= DateUtils.yesterday();
        reportForms.setDate(date);
        //用户充值提现明细
        List<H2AppUserRechargeWithdrawals> userRW = userRechargeService.getYesterdayRwDate(null);
        if (CollectionUtils.isNotEmpty(userRW)){
            //充值金额
            BigDecimal recharge=userRW.stream().filter(a->1 == (int)a.getAuditType() && a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getCode())) .map(H2AppUserRechargeWithdrawals::getRealAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
            reportForms.setRecharge(recharge);
            //首充金额
            BigDecimal firstCharge=userRW.stream().filter(a->1 == (int)a.getAuditType() && a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getCode()) && a.getNum()==1) .map(H2AppUserRechargeWithdrawals::getRealAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
            reportForms.setFirstCharge(firstCharge);
            //提现金额
            BigDecimal withdrawal=userRW.stream().filter(a->a.getType().equals(AccountBusinessTypeEnum.WITHDRAWAL.getCode())).map(H2AppUserRechargeWithdrawals::getAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
            reportForms.setWithdrawal(withdrawal);
            //充值人数
            Long rechargeNum=userRW.stream().filter(a->1 == (int)a.getAuditType() && a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getCode())).map(H2AppUserRechargeWithdrawals::getUserId).distinct().count();
            reportForms.setRechargeNum(null==rechargeNum?0:rechargeNum.intValue());
            //首充人数
            Long firstChargeNum=userRW.stream().filter(a->1 == (int)a.getAuditType() && a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getCode()) && a.getNum()==1).map(H2AppUserRechargeWithdrawals::getUserId).distinct().count();
            reportForms.setFirstChargeNum(null==firstChargeNum?0:firstChargeNum.intValue());
            //提现人数
            Long withdrawalNum=userRW.stream().filter(a->1 == (int)a.getAuditType() && a.getType().equals(AccountBusinessTypeEnum.WITHDRAWAL.getCode())).map(H2AppUserRechargeWithdrawals::getRealAmount).distinct().count();
            reportForms.setWithdrawalNum(null==withdrawalNum?0:withdrawalNum.intValue());
        }
        //店铺充值提现明细
        List<H2ShopRechargeWithdrawal> shopRW=rechargeWithdrawalMapper.getYesterdayRwDate(null);
        if (CollectionUtils.isNotEmpty(shopRW)){
            if (null==reportForms.getRecharge()){
                reportForms.setRecharge(BigDecimal.ZERO);
            }
            if (null==reportForms.getFirstCharge()){
                reportForms.setFirstCharge(BigDecimal.ZERO);
            }
            if (null==reportForms.getWithdrawal()){
                reportForms.setWithdrawal(BigDecimal.ZERO);
            }
            //充值金额
            BigDecimal recharge=shopRW.stream().filter(a->a.getStatus().equals("1") && a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getCode())) .map(H2ShopRechargeWithdrawal::getActualArrivalAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
            reportForms.setRecharge(recharge.add(reportForms.getRecharge()));
            //首充金额
            BigDecimal firstCharge=shopRW.stream().filter(a->a.getStatus().equals("1") && a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getCode()) && a.getFirstRecharge()==1) .map(H2ShopRechargeWithdrawal::getActualArrivalAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
            reportForms.setFirstCharge(firstCharge.add(reportForms.getFirstCharge()));
            //提现金额
            BigDecimal withdrawal=shopRW.stream().filter(a->a.getType().equals(AccountBusinessTypeEnum.WITHDRAWAL.getCode())).map(H2ShopRechargeWithdrawal::getAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
            reportForms.setWithdrawal(withdrawal.add(reportForms.getWithdrawal()));
            //充值店铺数
            Long rechargeShopNum=shopRW.stream().filter(a->a.getStatus().equals("1") && a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getCode())).map(H2ShopRechargeWithdrawal::getShopId).distinct().count();
            reportForms.setRechargeShopNum(rechargeShopNum.intValue());
            //提现店铺数
            Long withdrawalShopNum=shopRW.stream().filter(a->a.getStatus().equals("1") && a.getType().equals(AccountBusinessTypeEnum.WITHDRAWAL.getCode())).map(H2ShopRechargeWithdrawal::getShopId).distinct().count();
            reportForms.setWithdrawalShopNum(withdrawalShopNum.intValue());
            //首充店铺数
            Long firstChargeShopNum=shopRW.stream().filter(a->a.getStatus().equals("1") && a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getCode()) && a.getFirstRecharge()==1) .map(H2ShopRechargeWithdrawal::getShopId).distinct().count();
            reportForms.setFirstChargeShopNum(firstChargeShopNum.intValue());
        }
        //昨天注册用户
        List<H2AppUser> h2AppUsers=ih2AppUserService.getYesterday();
        reportForms.setRegisterNum(CollectionUtils.isEmpty(h2AppUsers)?0:h2AppUsers.size());
        //昨天注册店铺
        List<H2ShopBase> h2ShopBases=h2ShopBaseDao.getYesterday();
        reportForms.setRegisterShopNum(CollectionUtils.isEmpty(h2ShopBases)?0:h2ShopBases.size());
        //交易数量
        List<ShopOrderInfo> shopOrderInfos=shopOrderInfoService.getOrderYesterday();
        reportForms.setTransactionNum(CollectionUtils.isEmpty(shopOrderInfos)?0:shopOrderInfos.size());
        applicationContext.publishEvent(new OperateReportFormsEvent(reportForms));
    }
}
